package com.example.exp_3
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.exp_3.ui.theme.Exp3Theme

// 图片数据类
data class ImageData(
    val resId: Int,
    val title: String,
    val author: String,
    val description: String
)

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Exp3Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ImageGalleryScreen()
                }
            }
        }
    }
}

@Composable
fun ImageGalleryScreen() {
    // 图片数据 - 使用系统图标作为备用，确保应用可以运行
    val images = listOf(
        ImageData(
            R.drawable.image1, // 使用您项目中的图片资源
            "佛香阁",
            "cy",
            "敬请期待"
        ),
        ImageData(
            R.drawable.image2,
            "墙",
            "cy",
            "敬请期待"
        ),
        ImageData(
            R.drawable.image3,
            "风景",
            "cy",
            "敬请期待"
        ),
        ImageData(
            R.drawable.image4,
            "桥",
            "cy",
            "敬请期待"
        )
    )

    var currentImageIndex by remember { mutableStateOf(0) }
    val currentImage = images[currentImageIndex]

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        MainImageCard(
            imageData = currentImage,
            modifier = Modifier
                .fillMaxWidth()
                .height(300.dp)
        )

        Spacer(modifier = Modifier.height(10.dp))

        // 导航控制按钮
        NavigationButtons(
            onPreviousClick = {
                currentImageIndex = if (currentImageIndex - 1 < 0) {
                    images.size - 1
                } else {
                    currentImageIndex - 1
                }
            },
            onNextClick = {
                currentImageIndex = (currentImageIndex + 1) % images.size
            },
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(modifier = Modifier.height(20.dp))

        // 缩略图列表
        ThumbnailList(
            images = images,
            selectedIndex = currentImageIndex,
            onThumbnailClick = { index ->
                currentImageIndex = index
            },
            modifier = Modifier
                .fillMaxWidth()
                .height(80.dp)
        )

        // 图片位置指示器
        ImageIndicator(
            currentPosition = currentImageIndex + 1,
            totalCount = images.size,
            modifier = Modifier.padding(top = 16.dp)
        )
    }
}

@Composable
fun MainImageCard(
    imageData: ImageData,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier,
        elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
        shape = RoundedCornerShape(16.dp)
    ) {
        Box(modifier = Modifier.fillMaxSize()) {
            Image(
                painter = painterResource(id = imageData.resId),
                contentDescription = imageData.title,
                modifier = Modifier.fillMaxSize(),
                contentScale = ContentScale.Crop
            )

            // 图片信息覆盖层
            Column(
                modifier = Modifier
                    .align(Alignment.BottomStart)
                    .fillMaxWidth()
                    .background(Color.Black.copy(alpha = 0.7f))
                    .padding(16.dp)
            ) {
                Text(
                    text = imageData.title,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.White
                )
                Text(
                    text = "作者: ${imageData.author}",
                    fontSize = 14.sp,
                    color = Color.White.copy(alpha = 0.9f),
                    modifier = Modifier.padding(top = 4.dp)
                )
                Text(
                    text = "介绍: ${imageData.description}",
                    fontSize = 14.sp,
                    color = Color.White.copy(alpha = 0.9f),
                    modifier = Modifier.padding(top = 4.dp)
                )
            }
        }
    }
}

@Composable
fun NavigationButtons(
    onPreviousClick: () -> Unit,
    onNextClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        Button(
            onClick = onPreviousClick,
            modifier = Modifier.weight(1f)
        ) {
            Text("上一张")
        }

        Spacer(modifier = Modifier.width(16.dp))

        Button(
            onClick = onNextClick,
            modifier = Modifier.weight(1f)
        ) {
            Text("下一张")
        }
    }
}

@Composable
fun ThumbnailList(
    images: List<ImageData>,
    selectedIndex: Int,
    onThumbnailClick: (Int) -> Unit,
    modifier: Modifier = Modifier
) {
    Column(modifier = modifier) {
        Text(
            text = "图片预览",
            fontSize = 18.sp,
            fontWeight = FontWeight.Medium,
            modifier = Modifier.padding(bottom = 10.dp)
        )

        LazyRow(
            horizontalArrangement = Arrangement.spacedBy(18.dp)
        ) {
            items(images) { image ->
                val index = images.indexOf(image)
                ThumbnailItem(
                    imageData = image,
                    isSelected = index == selectedIndex,
                    onClick = { onThumbnailClick(index) }
                )
            }
        }
    }
}

@Composable
fun ThumbnailItem(
    imageData: ImageData,
    isSelected: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Card(
        onClick = onClick,
        modifier = modifier
            .size(60.dp)
            .clip(RoundedCornerShape(8.dp)),
        elevation = CardDefaults.cardElevation(
            defaultElevation = if (isSelected) 6.dp else 2.dp
        ),
        shape = RoundedCornerShape(8.dp),
        border = if (isSelected) {
            CardDefaults.outlinedCardBorder()
        } else {
            null
        }
    ) {
        Image(
            painter = painterResource(id = imageData.resId),
            contentDescription = "缩略图: ${imageData.title}",
            modifier = Modifier
                .fillMaxSize()
                .clip(RoundedCornerShape(8.dp)),
            contentScale = ContentScale.Crop
        )
    }
}

@Composable
fun ImageIndicator(
    currentPosition: Int,
    totalCount: Int,
    modifier: Modifier = Modifier
) {
    Text(
        text = "$currentPosition / $totalCount",
        fontSize = 14.sp,
        color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    Exp3Theme {
        ImageGalleryScreen()
    }
}